package metrics

import (
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promauto"
)

// 手动生成SSEFirstResponseDuration的桶：从5s开始，每200ms一个间隔
// 5s, 5.2s, 5.4s, ..., 直到需要的范围（这里示例到10s）
var sseFirstResponseBuckets = func() []float64 {
	var buckets []float64
	// 从5秒开始
	start := 5.0
	// 步长200ms
	step := 0.2
	// 生成25个桶，直到10秒（5 + 25*0.2 = 10）
	for i := 0; i <= 25; i++ {
		buckets = append(buckets, start+float64(i)*step)
	}
	return buckets
}()

// 手动生成SSEIntervalDuration的桶：从1s开始，每200ms一个间隔，直到5s
var sseIntervalBuckets = func() []float64 {
	var buckets []float64
	// 从1秒开始
	start := 1.0
	// 步长200ms
	step := 0.2
	// 生成21个桶，直到5秒（1 + 20*0.2 = 5）
	for i := 0; i <= 20; i++ {
		buckets = append(buckets, start+float64(i)*step)
	}
	return buckets
}()

// 定义指标
var (
	// 首次响应时延直方图
	SSEFirstResponseDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
		Name:    "sse_first_response_duration_seconds",
		Help:    "SSE first response duration in seconds",
		Buckets: sseFirstResponseBuckets,
	}, []string{"path"}) // 按路径标签区分

	// 响应间隔时延直方图
	SSEIntervalDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
		Name:    "sse_interval_duration_seconds",
		Help:    "SSE response interval duration in seconds",
		Buckets: sseIntervalBuckets,
	}, []string{"path"})
)
